package Day2025_02_25;

public class LeetCode6 {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        char[] ss = s.toCharArray();
        int n = ss.length;
        int d = 2 * numRows - 2;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < n; i += d) str.append(ss[i]);
        for (int k = 1; k < numRows - 1; k++) {
            for (int i = k, j = d - i; i < n || j < n; i += d, j += d) {
                if (i < n) str.append(ss[i]);
                if (j < n) str.append(ss[j]);
            }
        }
        for (int i = numRows - 1; i < n; i += d) str.append(ss[i]);
        return str.toString();
    }
}
